﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using wdss.WebServices;
namespace wdss.GUI
{
    public partial class DeleteDocument : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
            Response.Expires = -1500;
            Response.CacheControl = "no-cache";
            username_lbl.Text = (string)Session["username"];
            username_lbl.Visible = true;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Now);
            Response.Cache.SetNoServerCaching();

            Response.Cache.SetNoStore();

            if ((int)Session["role"] == 1)
            {
                Server.Transfer("~/GUI/Login.aspx");
            }
            if ((int)Session["role"] == 2)
            {
                Server.Transfer("~/GUI/DocumentList.aspx");
            }
            if ((int)Session["role"] == 3)
            {
                Server.Transfer("~/GUI/SystemAdministrator.aspx");
            }
        }

    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        if (ddl_ChooseDocument.SelectedItem != null)
        {
            LogService logAction = new LogService();
            string result = "Failure.";
            if (cb_Confirm.Checked)
            {
                DeleteDocumentService deleteDocument = new DeleteDocumentService();
                ListItem selectedItem = ddl_ChooseDocument.SelectedItem;
                int itemvalue = Int32.Parse(selectedItem.Value);
                try
                {
                    result = deleteDocument.DeleteDocument(Int32.Parse(ddl_ChooseDocument.SelectedValue));
                }
                catch (ArgumentNullException)
                {
                    lbl_Error.Visible = true;
                    lbl_Error.Text = result;
                }
                catch (FormatException)
                {
                    lbl_Error.Visible = true;
                    lbl_Error.Text = result;
                }
                catch (OverflowException)
                {
                    lbl_Error.Visible = true;
                    lbl_Error.Text = result;
                }

            }

            //handle failure
            if (result == "Failure.")
            {
                lbl_Error.Visible = true;
                lbl_Error.Text = "Could not delete document.";
            }
            //update documents
            if (result == "Success.")
            {
                logAction.LogAction("User has deleted the document " + ddl_ChooseDocument.SelectedValue + ".");
                lbl_Error.Visible = false;
                ddl_ChooseDocument.DataBind();
            }
            else
            {
                logAction.LogAction("User attempted to delete the documnt " + ddl_ChooseDocument.SelectedValue + " but an error prevented that.");
                lbl_Error.Text = "Selection is not confirmed.";
                lbl_Error.Visible = true;
            }
        }
        else
        {
            lbl_Error.Visible = true;
            lbl_Error.Text = "No document selected.";
        }
      }
        }
  }
